import {
	type IAttachGroup,
	type IAttachGroups,
	type IAttachUpdate,
	type IAttachs,
	attachGroupScheme,
	attachGroupsResponseScheme,
	attachScheme,
	attachsScheme,
	type forObject
} from '@/types/attach.types'

import { axiosWithAuth } from '@/api/interceptors'

export const attachService = {
	async fetchMany(for_object: forObject) {
		const response = await axiosWithAuth.get<IAttachs>('/attachments/', {
			params: { [for_object['name']]: for_object['id'] }
		})

		return attachsScheme.parse(response.data)
	},
	async del(id: number, for_object: forObject) {
		const response = await axiosWithAuth.delete(`/attachments/${id}/`, {
			params: { [for_object['name']]: for_object['id'] }
		})

		return response.status === 204
	},
	async update(id: number, data: IAttachUpdate) {
		const response = await axiosWithAuth.put(`/attachments/${id}/`, data)

		return attachScheme.parse(response.data)
	},
	async fetchManyGroup(name: string = '') {
		if (name) {
			const response = await axiosWithAuth.get<IAttachGroups>(
				'/attachments/groups/',
				{
					params: { name: name }
				}
			)

			return attachGroupsResponseScheme.parse(response.data)
		}
		const response = await axiosWithAuth.get<IAttachGroups>(
			'/attachments/groups/'
		)

		return attachGroupsResponseScheme.parse(response.data)
	},
	async createGroup(data: IAttachGroup) {
		const response = await axiosWithAuth.post<IAttachGroup>(
			'/attachments/groups/',
			data
		)

		return attachGroupScheme.parse(response.data)
	},
	async updateGroup(data: IAttachGroup) {
		const response = await axiosWithAuth.put<IAttachGroup>(
			`/attachments/groups/${data.id}/`,
			data
		)

		return attachGroupScheme.parse(response.data)
	},
	async delGroup(id: number) {
		const response = await axiosWithAuth.delete(`/attachments/groups/${id}/`)

		return response.status === 204
	}
	// async fetchRequest(id: string) {
	// 	const response = await axiosWithAuth.get<IRequest>(`/requests/${id}/`)
	//
	// 	return requestScheme.parse(response.data)
	// },
	// async updateRequest(id: number, data: IRequestForm) {
	// 	const response = await axiosWithAuth.put<IRequest>(`/requests/${id}/`, data)
	//
	// 	console.log(response.data)
	// 	return requestScheme.parse(response.data)
	// }
}
